package com.newtouch.xcd.cms.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newtouch.xcd.cms.entity.Trust;
import com.newtouch.xcd.cms.mapper.TrustMapper;
import com.newtouch.xcd.cms.service.TrustService;
import com.newtouch.xcd.cms.to.crm.CrmData;
import com.newtouch.xcd.common.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 信托立项 服务实现类
 * </p>
 *
 * @author dongyuefeng
 * @since 2020-05-21
 */
@Component
@Service
public class TrustServiceImpl extends ServiceImpl<TrustMapper, Trust> implements TrustService {

    @Autowired
    private TrustMapper trustMapper;

    @Override
    public List<Trust> queryByCustomId(String customId) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("customer_id", customId);
        queryWrapper.eq("status", 2);
        return this.list(queryWrapper);
    }

    @Override
    public Trust queryByXtNumber(String xtNumber) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("xt_number", xtNumber);

        return this.getOne(queryWrapper);
    }

    @Override
    public List<String> queryXtNumberList() {

        List<String> list = this.trustMapper.queryXtNumberList();
        return list;
    }

    /**
     * 资产配置
     * C1 = 支出项的资产配置
     * @param xtNumber
     * @param crmData
     * @return
     */
    @Override
    public BigDecimal calC1(String xtNumber,CrmData crmData){

        if(crmData == null){
            return BigDecimal.ZERO;
        }

        BigDecimal c1 = NumberUtils.formatNumber(crmData.getExpenditureItem().getAssetAllocation());
        return c1;
    }

    /**
     * 通过信托编号计算信托总财产（A）
     * A=B+C2-D-E-F
     *
     * @param xtNumber 信托编号
     * @return
     */
    @Override
    public BigDecimal calTotalValueOfTrustProperty(String xtNumber,CrmData crmData) {

        if(crmData == null){
            return BigDecimal.ZERO;
        }

        BigDecimal b = this.calInitialAdditionalTrustProperty(xtNumber,crmData);
        BigDecimal c2 = this.calC2(xtNumber,crmData);
        BigDecimal d = this.calCreditInsuranceFundOccupation(xtNumber,crmData);
        BigDecimal e = this.calTotalFees(xtNumber,crmData);
        BigDecimal f = this.calDistributionOfTrustBenefits(xtNumber,crmData);

        BigDecimal a = b.add(c2).subtract(d).subtract(e).subtract(f);

        return a;
    }

    /**
     * 初始/追加信托财产
     * 直接取收入项的-初始/追加信托财产
     * B（金额-合计本金）
     *
     * @param xtNumber
     * @return
     */
    @Override
    public BigDecimal calInitialAdditionalTrustProperty(String xtNumber,CrmData crmData) {

        if (crmData == null) {
            return BigDecimal.ZERO;
        }

        BigDecimal result = NumberUtils.formatNumber(crmData.getIncomeItem().getInitialAdditionalTrustProperty());
        return result;
    }

    /**
     * 查询-信托利益分配（已分配合计）
     * F（金额）
     *
     * @param xtNumber
     * @return
     */
    @Override
    public BigDecimal calDistributionOfTrustBenefits(String xtNumber,CrmData crmData) {

        if (crmData == null) {
            return BigDecimal.ZERO;
        }

        BigDecimal result = NumberUtils.formatNumber(crmData.getDistributionTrustBenefits().getTotal());
        return result;
    }

    /**
     * 费用合计（保护人费用、受托人费用、其他费用）
     * E（金额）
     *
     * @param xtNumber
     * @return
     */
    @Override
    public BigDecimal calTotalFees(String xtNumber,CrmData crmData) {

        if (crmData == null) {
            return BigDecimal.ZERO;
        }

        BigDecimal result = NumberUtils.formatNumber(crmData.getCost().getTotal());
        return result;
    }

    /**
     * 信保基金占用
     * D（金额）
     *
     * @param xtNumber
     * @return
     */
    @Override
    public BigDecimal calCreditInsuranceFundOccupation(String xtNumber,CrmData crmData) {

        if (crmData == null) {
            return BigDecimal.ZERO;
        }

        BigDecimal result = NumberUtils.formatNumber(crmData.getCreditInsuranceFundOccupiedFund().getTotal());
        return result;
    }


    /**
     * 信托的累计收益
     * 理财收益+股权投资收益+其他收益
     * C2（金额）
     *
     * @param xtNumber
     * @return
     */
    @Override
    public BigDecimal calC2(String xtNumber, CrmData crmData) {

        BigDecimal result = BigDecimal.ZERO;

        BigDecimal financialIncome = NumberUtils.formatNumber(crmData.getIncomeItem().getFinancialIncome());
        BigDecimal equityInvestmentIncome = NumberUtils.formatNumber(crmData.getIncomeItem().getEquityInvestmentIncome());
        BigDecimal otherIncome = NumberUtils.formatNumber(crmData.getIncomeItem().getOtherIncome());

        result = NumberUtils.add(financialIncome,equityInvestmentIncome,otherIncome);

//        result = NumberUtils.formatNumber(crmData.getAssetAllocation().getTotalGrossReceipts());

//        //往期产品列表
//        List<CrmAssetAllocationProductItem> pastProductItems = crmData.getAssetAllocation().getAssetAllocationStoragePeriod().getAssetAllocationProductList();
//        for (CrmAssetAllocationProductItem item : pastProductItems) {
//            List<CrmAssetAllocationProductIncomeItem> incomeItems = item.getIncomeList().stream().filter(a -> "1".equals(a.getArriveFlag())).collect(Collectors.toList());
//            BigDecimal incomeTotal = incomeItems.stream().map(a -> NumberUtils.formatNumber(a.getBenefitAmount())).reduce(BigDecimal.ZERO, BigDecimal::add);
//            result = NumberUtils.add(result,incomeTotal);
//        }
//
//        //往期产品列表
//        List<CrmAssetAllocationProductItem> productItems = crmData.getAssetAllocation().getAssetAllocationMaturity().getAssetAllocationProductList();
//        for (CrmAssetAllocationProductItem item : productItems) {
//            List<CrmAssetAllocationProductIncomeItem> incomeItems = item.getIncomeList().stream().filter(a -> "1".equals(a.getArriveFlag())).collect(Collectors.toList());
//            BigDecimal incomeTotal = incomeItems.stream().map(a -> NumberUtils.formatNumber(a.getBenefitAmount())).reduce(BigDecimal.ZERO, BigDecimal::add);
//            result = NumberUtils.add(result,incomeTotal);
//        }

        return result;
    }

}
